<template>
  <!--sell 0买入 1卖出 -->
  <div class="sell-group youke">
    <van-nav-bar
      :title="$route.query.sell==0?'买入':'卖出'"
      left-arrow
      fixed
      @click-left="onBack('/trading')"
    />
    <Main>
      <div class="sell-heads">
        <div class="sell-heads-img">
          <div class="heads-img">
            <img :src="requestParam.handImg" alt srcset>
          </div>
          <div class="heads-text">
            <div>
              单价:
              <span>{{requestParam.price}}</span>
            </div>
            <div>
              总数量:
              <span>{{requestParam.amount}}</span>锁定:
              <span>{{requestParam.suodin}}</span>
            </div>
          </div>
        </div>
        <div class="sell-heads-msg">
          <div>
            昵称:
            <span>{{requestParam.cname}}</span>
          </div>
          <div>
            电话:
            <span>{{requestParam.mobile}}</span>
          </div>
        </div>
      </div>
      <div class="sell-view">
        <div class="inputs-group">
          <van-field
            v-model="$store.getters.zhichans.ajf"
            label="可用资产"
            type="textarea"
            rows="1"
            readonly
            autosize
          >
            <span class="van-right-text" slot="right-icon">枚</span>
          </van-field>
        </div>
        <div class="inputs-group">
          <van-field
            v-model="requestParam.price"
            label="价格"
            type="textarea"
            rows="1"
            autosize
            readonly
            placeholder="1.1¥=1枚"
          >
            <span class="van-right-text" slot="right-icon">¥</span>
          </van-field>
          <van-field
            v-model="requestParam.amount"
            label="数量"
            type="textarea"
            rows="1"
            autosize
            readonly
            :placeholder="$route.query.sell==0?'买入数量':'卖出数量'"
          >
            <span class="van-right-text" slot="right-icon">枚</span>
          </van-field>
        </div>
        <div class="inputs-group">
          <van-field
            :value="$route.query.sell == 0 ?  requestParam.price * requestParam.amount :(requestParam.amount ? parseInt(requestParam.amount)  + parseFloat(requestParam.amount * rate)   :  '')"
            label="需支付"
            type="textarea"
            rows="1"
            autosize
            :placeholder="$route.query.sell == 1 ? '0.00 （包含'+ parseInt(rate*100) +'%手续费）': '0.00 （包含0.0%手续费）'"
            readonly
          >
            <span class="van-right-text" slot="right-icon">{{$route.query.sell==0?'¥':'枚'}}</span>
          </van-field>
        </div>
        <div class="inputs-group">
          <van-field
            v-model="requestParam.traderPass"
            label="交易密码"
            type="password"
            rows="1"
            autosize
            placeholder="请输入交易密码"
          />
        </div>
      </div>
      <div class="rule-group-tip">
        <div class="rule-tip-title"></div>
        <div class="rule-tip-content" v-html="rules"></div>
      </div>
    </Main>
    <footer-btn :text="$route.query.sell==0?'确定买入':'确定卖出'" @btn-click="sendFun"></footer-btn>
  </div>
</template>

<script type="text/ecmascript-6">
import footerBtn from "@/components/common/footerBtn";
import { trads, guide, games } from "@/api";

export default {
  //接收父级传递的参数
  props: [],
  //监听数据变化
  watch: {},
  data() {
    return {
      requestParam: {
        applicable: "300",
        price: 0.0,
        amount: 0,
        payment: 0.0,
        traderPass: "",
        mobile: "",
        cname: "",
        handImg: "",
        suodin: ""
      },
      rate: 0,
      rules: "",
      btndisable:true
    };
  },
  //实例初始化之前触发事件
  beforeCreate() {},
  //实例化之后触发事件
  created() {
    this.getlist();
  },
  //实时监控data参数数据变化
  computed: {},
  //模板组件
  components: {
    footerBtn
  },
  //执行方法
  methods: {
    sendFun() {
      let that = this;
      let _data = {
        jf: that.requestParam.amount,
        did: that.$route.query.id,
        passwo: that.requestParam.traderPass
      };
      //  if (!that.requestParam.price) {
      //   this.$toast.fail("价格不能为空");
      //   return false;
      // }
      // if (!that.requestParam.amount) {
      //   this.$toast.fail("数量不能为空");
      //   return false;
      // }
       if (!that.requestParam.traderPass) {
        this.$toast.fail("交易密码不能为空");
        return false;
      }

      // 挂卖
      if (!this.btndisable) {
        this.$toast.fail("点击太频繁");
        return false;
      }
      this.btndisable = false;
      let sell = that.$route.query.sell; //0是买入 1是卖出
      if (sell == 0) {
        trads.sell(_data).then(res => {
          that.$toast(res.reason);
          if (res.code == 1000) {
            setTimeout(function() {
              that.$router.push({ path: "/order/index" });
            }, 1000);
          }else{
            this.btndisable=true
          }
        });
      } else {
        trads.hang(_data).then(res => {
          that.$toast(res.reason);
          if (res.code == 1000) {
            setTimeout(function() {
              that.$router.push({ path: "/order/index" });
            }, 1000);
          }else{
            this.btndisable=true
          }
        });
      }
    },
    getlist() {
      trads.tradDetail({ did: this.$route.query.id }).then(res => {
        if (res.code == 1000) {
          this.requestParam.mobile = res.result.mobile;
          this.requestParam.cname = res.result.cname;
          this.requestParam.price = res.result.dj;
          this.requestParam.handImg = res.result.image;
          this.requestParam.suodin = res.result.suodin;
          this.requestParam.amount = res.result.jfs;
        }
      });
    },
    // 交易规则
    jyrule() {
      guide.jyrule().then(res => {
        if (res.code == 1000) {
          this.rules = res.result;
        }
      });
    }
  },
  mounted() {
    if (1 == this.$route.query.sell) {
      trads
        .rate()
        .then(ret => {
          this.rate = ret.result;
        })
        .catch(err => {});
    }
    this.jyrule();
    this.$store.dispatch("addzhichan")
  }
  //路由退出时候调用
  //beforeRouteLeave(to, from, next){next()}
};
</script>

<style scoped lang="less">
.sell-group {
  .sell-heads {
    background: #ffffff;

    .sell-heads-img {
      display: flex;
      align-items: center;
      padding: 1rem;

      .heads-img {
        width: 3.75rem;
        height: 3.75rem;
        border-radius: 50%;
        background: #000;
        margin-right: 1rem;

        img {
          width: 3.75rem;
          height: 3.75rem;
          border-radius: 50%;
        }
      }

      .heads-text {
        display: flex;
        height: 3.75rem;
        justify-content: space-around;
        flex-direction: column;
        font-size: 0.9625rem;

        span {
          display: inline-block;
          margin: 0 0.4rem;
        }
      }
    }

    .sell-heads-msg {
      display: flex;
      padding: 1rem;
      justify-content: space-between;
      position: relative;
      font-size: 0.9625rem;

      &::before {
        position: absolute;
        content: "";
        width: 90%;
        left: 5%;
        top: 0;
        border-bottom: 1px solid #ebedf0;
      }
    }
  }

  .rule-group-tip {
    padding: 0rem 1rem;
    margin-top: 1rem;

    .rule-tip-title {
      text-align: center;
      padding: 0.5rem 0;
    }

    .rule-tip-content {
      font-size: 0.9625rem;
    }
  }
}
</style>
